package com.boot.config.rpc

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.net.MalformedURLException
import java.net.URL

/**
 * JSON-RPC Client
 */
@Configuration
class JsonRpcClientConfig {

    @Bean
    @ConditionalOnProperty(value = ["rpc.client.url", "rpc.client.basePackage"])
    fun rpcClientProxyCreator(@Value("\${rpc.client.url}") url: String?, @Value("\${rpc.client.basePackage}") basePackage: String?): AutoJsonRpcClientProxyCreator? {
        val creator = AutoJsonRpcClientProxyCreator()
        try {
            creator.setBaseUrl(URL(url))
        } catch (e: MalformedURLException) {
            log.error("创建RPC服务地址错误: {}", e)
            e.printStackTrace()
        }
        creator.setScanPackage(basePackage)
        return creator
    }

    companion object {
        private val log: Logger = LoggerFactory.getLogger(this::class.java)
    }

}